﻿using UnityEngine;
using System.Collections;
using COC.Character;
using COC.CComponent;

[RequireComponent(typeof(GUIText))]
public class HealthBarComponent : BaseComponent
{
		
		public Font mFont;
		public Transform target; //Object that this label should follow
		public Vector3 offset = Vector3.up;//Units in world space to offset; 1 unit above object by default
		public bool clampToScreen = false;//If true,label will be visible even if object is off screen
		public float clampBoardersize = 0.05f; //How much viewport space to leave at the boarder when a label is being clamped
		public bool useMainCamera = true;
		public Camera cameraToUse;//Only use this if useMainCamera is false
		Camera cam;
		Transform camTransform;
		public Transform currentTransform;//this object is a follower that following to target position.
		TextMesh showValue;
		MeshRenderer mMeshRenderer;
		Health health;
		GameObject healthBar;
		Vector3 originPos;
		Vector3 targetPos;
		int mFontSize = 20;
		float consume = 2;
		float scaleX = 25;
		float scaleY = 25;
		float scaleZ = 25;
	    
		//	private float startTime;
		//	private float endTime;
		//	private float distCovered;
		//	private float journeyLength;
		//	private float speed=2.0F;
		//	private float fracJourney ;

		void Start ()
		{
		        
				if (health == null) {
						health = new Health ();
				}


				health.currHealth = 100;
				///regist text mesh for modify status value dynamically,and mesh renderer.    
				healthBar = new GameObject ("HealthBar" + "_" + System.Guid.NewGuid ());
				showValue = healthBar.AddComponent<TextMesh> ();
				//mMeshRenderer = healthBar.AddComponent<MeshRenderer> ();
				///configurate mesh text and mesh renderer 
				//mMeshRenderer.material=Material.
				//healthBar.transform.position=this.transform.position;
				healthBar.transform.parent = this.transform;
				showValue.transform.parent=healthBar.transform;
				healthBar.transform.localScale = new Vector3 (scaleX, scaleY, scaleZ);
				showValue.offsetZ = -1f;
				///////////////////////////NOT FINISH//////////////////////////////////////////
				if (mFont != null) {
						showValue.font = mFont;
				} else {
						showValue.font = GUI.skin.font; //maybe add default font from custom gui skin
				}
				showValue.characterSize = 1f;
				showValue.tabSize = 4f;
				showValue.alignment = TextAlignment.Center;
				showValue.anchor = TextAnchor.MiddleCenter;
				showValue.fontSize = mFontSize;
				showValue.color = Color.blue;
				showValue.fontStyle = FontStyle.Bold;
		        
				///////////////////////////NOT FINISH////////////////////////////////////////  
				healthBar.renderer.material = showValue.font.material;//copy the font material to game object,then game object as a text.
				if (useMainCamera)
						cam = Camera.main;
				else
						cam = cameraToUse;
				camTransform = cam.transform;
				if(currentTransform==null)
				currentTransform = healthBar.transform;
				target = this.transform;
				///simulating live health
				InvokeRepeating ("ChangeHealthValue", 3f, 0.3f);
		}
	
		void ChangeHealthValue ()
		{
				if (health != null) {
						health.currHealth = (health.currHealth == 0) ? 0 : health.currHealth - consume;
						Debug.Log ("parent.position.x:" + this.transform.position.x + " ; current position x:" + healthBar.transform.position.x);
				}
		}

		void Update ()
		{
				//Hook up your health variable here.


				if (clampToScreen) {
						Vector3 relativePosition = camTransform.InverseTransformPoint (target.position);
						relativePosition.z = Mathf.Max (relativePosition.z, 1.0f);
						currentTransform.position = cam.WorldToViewportPoint (camTransform.TransformPoint (relativePosition + offset));
						currentTransform.position = new Vector3 (Mathf.Clamp (currentTransform.position.x, clampBoardersize, 1.0f - clampBoardersize), 
			                                         Mathf.Clamp (currentTransform.position.y, clampBoardersize, 1.0f - clampBoardersize),
			                                         currentTransform.position.z);
						
				} else {
						currentTransform.position = cam.WorldToViewportPoint (target.position + offset);
				}
				healthBar.transform.position = currentTransform.position;
				showValue.text = "FJEEEEEEEEEEEEEEEEEEEEEEEE HP:" + health.currHealth;
		}
	   
		public HealthBarComponent GetHealthBarInstance ()
		{
				return this;
		}

		public override void OnDestroy ()
		{
				CancelInvoke ("ChangeHealthValue");
		}
		class Health
		{
				public float currHealth{ get; set; }

				public float maxHealth{ get { return 100f; } set { value = maxHealth; } }

		}


}
